package com.xy.exam.service.impl;

import com.xy.exam.Exception.ServiceException;
import com.xy.exam.dao.mapper.FeedbackMapper;
import com.xy.exam.pojo.dto.FeedbackAddNewDTO;
import com.xy.exam.pojo.dto.FeedbackUpdateDTO;
import com.xy.exam.pojo.entity.Feedback;
import com.xy.exam.pojo.vo.FeedbackListItemVO;
import com.xy.exam.pojo.vo.FeedbackStandardVO;
import com.xy.exam.service.IFeedBackService;
import com.xy.exam.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FeedBackServiceImpl implements IFeedBackService {
    @Autowired
    FeedbackMapper feedbackMapper;
    @Override
    public void addNew(FeedbackAddNewDTO feedbackAddNewDTO) {
        String title = feedbackAddNewDTO.getTitle();
        int countByTitle = feedbackMapper.countByTitle(title);
        if (countByTitle > 0) {
            String message = "添加反馈信息失败,该反馈信息标题已经存在!";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        Feedback feedback = new Feedback();
        feedback.setStatus(0);
        feedback.setCreateTime(new Date());
        BeanUtils.copyProperties(feedbackAddNewDTO, feedback);
        int insert = feedbackMapper.insert(feedback);
        if (insert != 1) {
            String message = "添加反馈信息失败,该反馈信息标题已经存在!";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        FeedbackStandardVO standardById = feedbackMapper.getStandardById(id);
        if (standardById == null) {
            String message = "删除反馈信息失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        int deleteById = feedbackMapper.deleteById(id);
        if (deleteById !=1) {
            String message = "删除反馈信息失败，服务器忙，请稍后再尝试!";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateFeedbackById(Long id, FeedbackUpdateDTO feedbackUpdateDTO) {
        FeedbackStandardVO standardById = feedbackMapper.getStandardById(id);
        if (standardById == null) {
            String message = "修改反馈信息详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        String name = feedbackUpdateDTO.getTitle();
        int count = feedbackMapper.countByTitleAndNotId(id,name);
        if (count > 0) {
            String message = "修改反馈信息详情失败，反馈信息名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Feedback feedback = new Feedback();
        feedback.setId(id);
        BeanUtils.copyProperties(feedbackUpdateDTO,feedback);
        int rows = feedbackMapper.update(feedback);
        if (rows != 1) {
            String message = "修改反馈信息详情失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public FeedbackStandardVO getStandardById(Long id) {
        FeedbackStandardVO standardById = feedbackMapper.getStandardById(id);
        if (standardById == null) {
            String message = "获取用户详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return standardById;
    }

    @Override
    public List<FeedbackListItemVO> list() {
        List<FeedbackListItemVO> list = feedbackMapper.list();
        return list;
    }
}
